package com.heima.test15;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        //1 创建服务器的socket对象 运行在10000端口
        ServerSocket ss = new ServerSocket(10000);

        //2 接收客户端的连接
        System.out.println(11);
        Socket accept = ss.accept();//会等待连接
        System.out.println(22);

        //3 用accept接收客户端发来的数据
        InputStream is = accept.getInputStream();
        byte[] bytes = new byte[5];
        int len;
        while ((len = is.read(bytes))!=-1){
            //打印接收的数据
            System.out.println(new String(bytes, 0, len));
        }
        System.out.println("================");

        //4 给客户端回复信息
        OutputStream os = accept.getOutputStream();
        String s = "你好  hello aaaa高大上根深蒂固发热三国杀";
        os.write(s.getBytes());
        accept.shutdownOutput();

        os.close();
        is.close();
        accept.close();
        ss.close();//服务器关闭
    }
}
